import type { AxiosRequestConfig } from "axios";
import service from "./helpers/axios-instance";
import * as pendingManager from "./helpers/pendingManager";

export function createRequest<T = any>(config: AxiosRequestConfig) {
  return service.request<any, T>(config);
}

export const http = {
  get: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
    createRequest<T>({ ...config, url, params: data, method: "GET" }),

  post: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
    createRequest<T>({ ...config, url, data, method: "POST" }),

  put: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
    createRequest<T>({ ...config, url, data, method: "PUT" }),

  delete: <T = any>(url: string, config?: AxiosRequestConfig) => createRequest<T>({ ...config, url, method: "DELETE" }),

  cancelAll: pendingManager.cancelAll,
  cancel: pendingManager.cancel
};

export default http;
